package org.example.DesignPatterns.Command;

/**
 * @description:
 * @author: farprt
 * @create: 2022-09-24 11:02
 **/
public class FlashCommand implements Command {

    private Bulb bulb;
    private volatile boolean neonRun=false;//闪烁命令运行状态

    public FlashCommand(Bulb bulb) {
        this.bulb = bulb;
    }

    @Override
    public void exe() {
        if(!neonRun){
            neonRun=true;
            System.out.println("霓虹灯闪烁任务启动");
            new Thread(()->{
                try {
                    while (neonRun){
                        bulb.on();
                        Thread.sleep(500);
                        bulb.off();
                        Thread.sleep(500);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

    }

    @Override
    public void unexe() {
        neonRun=false;
        System.out.println("霓虹灯闪烁任务结束");
    }
}